package org.linlinjava.litemall.db.config;

import java.util.List;

import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.InterceptorContext;
import org.beetl.sql.core.engine.SQLParameter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ItextInterceptor implements Interceptor {
	private static final ObjectMapper mapper = new ObjectMapper();

	@Override
	public void before(InterceptorContext ctx) {
		List<SQLParameter> sqlParameters = ctx.getParas();
		for (SQLParameter sqlParameter : sqlParameters) {
			if(sqlParameter.value.getClass().isArray() || sqlParameter.value instanceof JsonNode) {
				try {
					sqlParameter.value = mapper.writeValueAsString(sqlParameter.value);
				} catch (JsonProcessingException e) {
					throw new RuntimeException(e);
				}
			}
		}
		
	}

	@Override
	public void after(InterceptorContext ctx) {
		
	}

	@Override
	public void exception(InterceptorContext ctx, Exception ex) {
		
	}
	
}
